package com.sunlands.zlcx.usercenter.config;

import lombok.Data;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpTemplateMsgServiceImpl;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author anjl
 * @date 8:15 PM 2018/12/19
 * description:
 */
@Data
@ConfigurationProperties(prefix = "wechat")
@Component
public class WeChatMpConfig {

    private String mpAppId;
    private String mpAppSecret;

    private String appAppId;
    private String appAppSecret;

    private String appPostgraduateAppId;
    private String appPostgraduateAppSecret;


    @Bean(name = "wxMpService")
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean(name = "wxAppService")
    public WxMpService wxAppService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxAppConfigStorage());
        return wxMpService;
    }

    /**
     * 考研自习室微信登录时使用
     * @return
     */
    @Bean(name = "wxAppPostgraduateService")
    public WxMpService wxAppPostgraduateService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxAppPostgraduateConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpTemplateMsgService WxMpTemplateMsgService(WxMpService wxMpService){
        WxMpTemplateMsgService wxMpTemplateMsgService = new WxMpTemplateMsgServiceImpl(wxMpService);
        return wxMpTemplateMsgService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(mpAppId);
        wxMpConfigStorage.setSecret(mpAppSecret);
        return wxMpConfigStorage;
    }

    @Bean
    public WxMpConfigStorage wxAppPostgraduateConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(appPostgraduateAppId);
        wxMpConfigStorage.setSecret(appPostgraduateAppSecret);
        return wxMpConfigStorage;
    }


    @Bean
    public WxMpConfigStorage wxAppConfigStorage() {
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(appAppId);
        wxMpConfigStorage.setSecret(appAppSecret);
        return wxMpConfigStorage;
    }
}

